package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;
    @Override
    public void queryPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        page = roomInfoMapper.queryPage(page,queryVo);
    }

    @Override
    public RoomDetailVo queryDetail(Long id,Integer userId) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if(roomInfo == null){
            return null;
        }
        ApartmentItemVo apartmentItemVo = apartmentInfoService.queryapartmentItemVoById(roomInfo.getApartmentId());
        List<GraphVo> list1 =  graphInfoMapper.queryByVoItemId(id,ItemType.ROOM);
        List<AttrValueVo> list2  = roomAttrValueMapper.queryByRoomId(id);
        List<FacilityInfo> list3 = facilityInfoMapper.queryByRoomId(id);
        List<LabelInfo> list4 = labelInfoMapper.queryByRoomId(id);
        List<PaymentType> list5 = paymentTypeMapper.selectByRoomId(id);
        List<FeeValueVo> list6 = apartmentFeeValueMapper.queryByApartmentId(roomInfo.getApartmentId());
        List<LeaseTerm> list7 = roomLeaseTermMapper.queryByRoomId(id);
        Boolean isCheckIn = roomInfoMapper.isCheckInByroomId(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(list1);
        roomDetailVo.setAttrValueVoList(list2);
        roomDetailVo.setFacilityInfoList(list3);
        roomDetailVo.setLabelInfoList(list4);
        roomDetailVo.setPaymentTypeList(list5);
        roomDetailVo.setFeeValueVoList(list6);
        roomDetailVo.setLeaseTermList(list7);
        roomDetailVo.setIsCheckIn(isCheckIn);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        browsingHistoryService.saveHistory(id,userId);
        return roomDetailVo;
    }

    @Override
    public void selectRoomListByApartmentId(Page<RoomItemVo> page, Long id) {
         page = roomInfoMapper.selectRoomByApartmentId(page,id);
    }
}




